﻿@using DragonSolution.Web.Common.Common;
<link href="~/Areas/AdminMrg/Content/adminCss/dialog.css" rel="stylesheet" />
 <!--树控件-->
<link href="~/CommonResource/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<script src="~/CommonResource/zTree/js/jquery.ztree.core-3.5.min.js"></script> 
<script src="~/CommonResource/zTree/js/jquery.ztree.excheck-3.5.min.js"></script>

<div class="ztree" id="treeUl" ></div>

<script type="text/javascript">
    var pageSize=@ConstDim.AdminPageSize;
    var treeObj = null;
    var rootNode=null;
    var rootChkDisabled=true;
    var currentId;
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            nocheckInherit: true,//入子节点继承父节点 nocheck = true 的属性
            chkStyle: "radio",
            radioType: "all"
        },
        callback: {
            //回调方法
            onClick: onClick,
            beforeCheck: beforeCheck,
            onCheck:onCheck
        }
    };
    //radio选择事件
    function onCheck(event, treeId, treeNode) {
        GetCheckedNodeId();//获取radio选中的节点id值
    }
    //事件回调函数,可以控制是否允许更改节点勾选状态
    function beforeCheck(treeId, treeNode) {
        return true;
    }
    //树节点点击事件
    function onClick(event, treeId, treeNode, clickFlag) {
        clickNode(treeNode);
    }

    //初始化
    $(function () {
        rootChkDisabled='@ViewBag.rootChkDisabled';
        var currentName='@ViewBag.cName';
        var currentParentId='@ViewBag.cParentId';
        if(currentParentId==""){
            //添加
            getCategoryList("");
        }
        else{
            //编辑(编辑时不能构选自身类别)
            $("#typeName").val(currentName);
            currentId='@ViewBag.cId';
            getCategoryList(currentParentId);
        }
    });

    //树形菜单（异步）
    function getCategoryList(parentId){
        var params = {};
        params.action = "GetCategoryList";
        $.ajax({
            url: "/Areas/AdminMrg/Ashx/Mnews.ashx",
            data: params,
            type: "POST",
            dataType: "json",
            success: function (msg) {
                if (msg.OperateResult == "操作成功"){
                    var treeJson = msg.ResponseData;
                    treeObj = $.fn.zTree.init($("#treeUl"), setting, treeJson);
                    
                    //跟节点
                    rootNode = treeObj.getNodes()[0];
                    if(rootChkDisabled){
                        //添加新闻时，根节点不可选择，添加新闻类型时，可以选择
                        treeObj.setChkDisabled(rootNode, true);
                    }

                    var chooseNode;
                    if(parentId=="")
                    {
                        //默认选中第一个节点
                        chooseNode=rootNode;
                    }
                    else{
                        //parentId不为空，选中此类别
                        var chooseNodes = treeObj.getNodesByParam("id", parentId);
                        if(chooseNodes.length>0){ chooseNode=chooseNodes[0]; }
                    }

                    //编辑时不能构选自身
                    if(currentId!=""){
                        var myself = treeObj.getNodesByParam("id", currentId);
                        if(myself.length>0){ 
                            //treeObj.setChkDisabled(myself[0], true);
                            myself[0].nocheck=true;
                        }
                    }

                    treeObj.selectNode(chooseNode);
                    setNodeCheck(chooseNode);
                    clickNode(chooseNode);

                }
            },
            error: function (e) {
                alert("加载出错");
            }
        });
    }

    //设置radio是否选中
    function setNodeCheck(chooseNode){
        treeObj.checkNode(chooseNode, true, false, true);//勾选,回调beforeCheck
        //treeObj.expandNode(cpNodes[0],true);//展开本节点
        //获取当前节点的父节点并且展开父节点
        var parentNode = chooseNode.getParentNode();
        if(parentNode!=null){
            treeObj.expandNode(parentNode,true);
        }
    }

    //点击节点事件
    function clickNode(treeNode){
        if(treeNode.open){
            treeObj.expandNode(treeNode,false);
        } else{
            treeObj.expandNode(treeNode,true);
        }
    }

    //获取radio选中的节点id值
    function GetCheckedNodeId() {
        var checkedNodes = treeObj.getCheckedNodes(true);
        if(checkedNodes.length>0){
            $("#parentTypeId").val(checkedNodes[0].id);
            $("#parentTypeName").val(checkedNodes[0].name);
        }
    }
</script>
